<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="发送人" prop="sendNickName">
                <el-input v-model="queryParams.sendNickName" placeholder="请输入发送用户名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="责任人" prop="receiveNickName">
                <el-input v-model="queryParams.receiveNickName" placeholder="请输入责任人用户名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="任务标题" prop="title">
                <el-input v-model="queryParams.title" placeholder="请输入任务标题" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="单据编码" prop="billCode">
                <el-input v-model="queryParams.billCode" placeholder="请输入单据编码" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="进度状态" prop="scheduleStatus">
                <el-select v-model="queryParams.scheduleStatus" placeholder="请选择进度状态" clearable>
                    <el-option v-for="dict in dict.type.progress_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['oa:sendTask:add']">新增</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['oa:sendTask:export']">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table  :border="Global.tableShowBorder" v-loading="loading" :data="sendTaskList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="发送人" align="center" prop="sendNickName" width="120"/>
                <el-table-column label="责任人" align="center" prop="receiveNickName" width="120"/>
                <el-table-column label="任务标题" align="center" prop="title" />
                <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="流程状态" align="center" prop="status" width="120">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.oa_flow_billstatus" :value="scope.row.status"/>
                    </template>
                </el-table-column>
                <el-table-column label="截止时间" align="center" prop="finishTime" width="180">
                    <template slot-scope="scope">
                        <span v-html="compareFinishTime(scope.row.finishTime)"></span>
                    </template>
                </el-table-column>
                <el-table-column label="进度" align="center" prop="schedule" width="120">
                    <template slot-scope="scope">
                        <div style="color: red;font-weight: bold;">{{scope.row.schedule}}%</div>
                    </template>
                </el-table-column>
                <el-table-column label="进度状态" align="center" prop="scheduleStatus" width="120">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.progress_status" :value="scope.row.scheduleStatus"/>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-check" @click="doSubmitFlow(scope.row)" v-hasPermi="['oa:sendTask:edit']">提交审批</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa:sendTask:edit']">修改</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:sendTask:remove']">删除</el-button>

                        <el-button size="mini" type="text" v-show="scope.row.status=='1'"  icon="el-icon-refresh-right" @click="doCancleFlow(scope.row)" v-hasPermi="['oa:sendTask:edit']" style="color: #ff6666">撤销申请</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status!='0'"  icon="el-icon-rank"          @click="flowDetail(scope.row)" v-hasPermi="['oa:sendTask:edit']">流程信息</el-button>
                        <el-button size="mini" type="text" v-show="scope.row.status!='0'"  icon="el-icon-view"          @click="handleView(scope.row)" v-hasPermi="['oa:sendTask:edit']">查看</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
        </TableCard>


        <FlowInfo :businessId="businessId" :billType="billType" v-if="showFlowInfo" :showStatus="showFlowInfo" @closeFlowInfoDialog="closeFlowInfoDialog"></FlowInfo>

    </div>
</template>

<script>
import { listSendTask, getSendTask, delSendTask, addSendTask, updateSendTask } from "@/api/oa/sendTask";
import SelectOneUser from "@/views/common/selectUser/selectOneUser"
import SelectMulUser from "@/views/common/selectUser/selectMulUser"

import FlowInfo from "@/views/workflow/flow/flowInfo";
import {cancleFlow, submitFlow} from "@/api/workflow/processInstance";

export default {
    name: "SendTask",
    dicts: ['oa_flow_billstatus', 'progress_status'],
    components:{SelectOneUser,SelectMulUser,FlowInfo},
    data() {
        return {
            billType:"",
            showFlowInfo:false,
            businessId:null,
            //选择用户弹窗
            showSelectOneUser:false,
            showSelectMulUser:false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 任务分配表格数据
            sendTaskList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn:"createTime",
                isAsc:"descending",
                sendUserId: null,
                sendNickName: null,
                receiveUserId: null,
                receiveNickName: null,
                title: null,
                content: null,
                extUserId: null,
                extNickName: null,
                billType: null,
                billCode: null,
                flowKey: null,
                flowInsId: null,
                submitTime: null,
                status: null,
                tenantId: null,
                schedule: null,
                scheduleStatus: null
            },
        };
    },
    created() {
        const billType = this.$route.query && this.$route.query.billType;
        console.log("单据类型："+billType)
        if(billType){
            this.queryParams.billType = billType;
            this.billType = billType;
        }
        this.getList();
    },
    methods: {
        compareFinishTime(finishTime){
            var starttime = new Date();
            var endtime = new Date(Date.parse(finishTime));
            if(starttime>endtime){
                return "<span style='color: red'>"+finishTime+"[超期]</span>"
            }else{
                return "<span style='color: green'>"+finishTime+"[正常]</span>"
            }
        },
        /** 查询任务分配列表 */
        getList() {
            this.loading = true;
            listSendTask(this.queryParams).then(response => {
                this.sendTaskList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$router.push({path: "/oa/sendTask/sendTask/form/add/"+this.billType})
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id
            this.$router.push({path: "/oa/sendTask/sendTask/form/" + row.id});
        },
        /** 查看单据详情 */
        handleView(row){
            this.$router.push({path: "/oa/sendTask/sendTask/form/" + row.id, query: {readonly:"1"}})
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除任务分配编号为"' + ids + '"的数据项？').then(function() {
                return delSendTask(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/sendTask/export', {
                ...this.queryParams
            }, `sendTask_${new Date().getTime()}.xlsx`)
        },



        /** 提交审批 ***/
        doSubmitFlow(row){
            let data = {
                businessId:row.id,
                billType: row.billType
            }
            this.$modal.confirm('确认要提交该单据进行审批吗？').then( ()=> {
                return submitFlow(data);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("提交成功");
            }).catch(() => {
            });
        },
        /** 取回审批 ***/
        doCancleFlow(row){
            let data = {
                businessId:row.id,
                billType: row.billType
            }
            this.$modal.confirm('确认要取回该协议审批吗？').then( ()=> {
                cancleFlow(data).then(res=>{
                    if(200==res.code){
                        this.getList();
                        this.$modal.msgSuccess("取回成功");
                    }
                });
            })
        },
        /** 流程信息 ***/
        flowDetail(row){
            this.businessId = row.id+"";
            this.showFlowInfo = true;
        },
        //关闭流程信息窗口
        closeFlowInfoDialog(){
            this.showFlowInfo = false;
        },
    }
};
</script>
